import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

import RouterGenerator from '../utils/RouterGenerator'

Vue.use(Router);

let dynamic = [];

let menus = store.state.account.user ? store.state.account.user.menus : [];

RouterGenerator(dynamic, menus);

const router = new Router ({
	mode: 'hash',
	linkActiveClass: 'active',
	routes: [
		{
			path: '/admin',
			component: () => import('../views/Admin.vue'),
			meta: {
				title: '控制台',
				requiresAuth: true
			},
			redirect: '/admin/dashboard',
			children: [
				...dynamic
			]
		},
		{
			path: '/login',
			component: () => import('../views/Login.vue'),
			beforeEnter: (to, from, next) => {
				/**
				 * 验证用户是否登录
				 */
				if (store.state.account.access_token) {
					router.replace({
						path: '/'
					})
				} else {
					next();
				}
			}
		},
		{
			path: '/',
			beforeEnter: (to, from, next) => {
				/**
				 * 验证用户是否登录
				 */
				if (store.state.account.access_token) {
					router.replace({
						path: '/admin/dashboard'
					})
				} else {
					router.replace({
						path: '/login'
					})
				}
			}
		},
		{
			path: '*',
			component: () => import('../views/errors/NotFound.vue')
		}
	]
});

export default router;
